﻿Public Class frmStudentInfoModify
    Dim mybind As BindingManagerBase
    '装载窗体
    Private Sub frmStudentInfoModify_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim mytable As Data.DataTable
        Dim recordnum As Integer
        cboSex.Items.Add("男")
        cboSex.Items.Add("女")
        cboMZ.Items.Add("是")
        cboMZ.Items.Add("否")
        txtSQL = "SELECT * FROM 学生"
        recordnum = ExecuteSQL(txtSQL, ErrorMsg)
        mytable = DBSet.Tables.Item(0)
        txtNum.DataBindings.Add("Text", mytable, "学号")
        txtName.DataBindings.Add("Text", mytable, "姓名")
        cboSex.DataBindings.Add("Text", mytable, "性别")
        cboMZ.DataBindings.Add("Text", mytable, "少数民族否")
        txtJG.DataBindings.Add("Text", mytable, "籍贯")
        txtBirthday.DataBindings.Add("Text", mytable, "出生日期")
        txtScore.DataBindings.Add("Text", mytable, "入学成绩")
        txtZY.DataBindings.Add("Text", mytable, "专业名称")
        txtPhoto.DataBindings.Add("Text", mytable, "照片")
        txtWebPage.DataBindings.Add("Text", mytable, "主页")
        txtResume.DataBindings.Add("Text", mytable, "简历")
        mybind = CType(Me.BindingContext(mytable), CurrencyManager)
        mybind.Position = 0
        txtNum.ReadOnly = True
    End Sub
    Private Sub cmdFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFirst.Click
        mybind.Position = 0
    End Sub
    Private Sub cmdBackward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBackward.Click
        If (mybind.Position = 0) Then
            MessageBox.Show("已经到了第一条记录！", "信息提示！", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            mybind.Position = mybind.Position - 1
        End If
    End Sub
    Private Sub cmdForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdForward.Click
        If mybind.Position = mybind.Count - 1 Then
            MessageBox.Show("已经到了最后一条记录！"， "信息提示！", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            mybind.Position = mybind.Position + 1
        End If
    End Sub
    Private Sub cmdLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLast.Click
        mybind.Position = mybind.Count - 1
    End Sub
    Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
        Dim recordnum As Integer
        Dim i As Integer = mybind.Position
        If txtBirthday.Text = "" Then
            MsgBox("请输入生日日期"， vbOKOnly, "警告")
            txtBirthday.Focus()
            Exit Sub
        End If

        If Not IsDate(txtBirthday.Text) Then
            MsgBox("日期的正确格式应该为（YYYY-MM-DD）", vbOKOnly, "警告")
            txtBirthday.Focus()
            Exit Sub
        End If

        If txtName.Text = " " Then
            MsgBox("请输入姓名"， vbOKOnly, "警告")
            txtName.Focus()
            Exit Sub
        End If
        txtSQL = "UPDATE 学生 SET "
        txtSQL = txtSQL & "姓名='" & txtName.Text & "',"
        txtSQL = txtSQL & "姓别='" & cboSex.Text & "',"
        txtSQL = txtSQL & "少数民族否='" & cboMZ.Text & "',"
        txtSQL = txtSQL & "籍贯='" & txtJG.Text & "',"
        txtSQL = txtSQL & "出身日期='" & txtBirthday.Text & "',"
        txtSQL = txtSQL & "入学成绩='" & txtScore.Text & "',"
        txtSQL = txtSQL & "专业名称='" & txtZY.Text & "',"
        txtSQL = txtSQL & "照片='" & txtPhoto.Text & "'"
        txtSQL = txtSQL & "主页='" & txtWebPage.Text & "'"
        txtSQL = txtSQL & "简历='" & txtResume.Text & "'"
        txtSQL = txtSQL & "WHERE 学号'" & txtNum.Text & "'"
        recordnum = ExecuteSQL(txtSQL, ErrorMsg)
        If recordnum > 0 Then
            MsgBox("更新完成！")
        Else
            MsgBox("更新失败！" & ErrorMsg)
        End If

        DBSet.Tables(0).Rows(mybind.Position).EndEdit()
        DBSet.Tables(0).AcceptChanges()
        mybind.Position = i
    End Sub

    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        Dim recordnum As Integer
        txtSQL = "DELETE 学生 WHERE 学号=" & txtNum.Text
        recordnum = ExecuteSQL(txtSQL, ErrorMsg)

        DBSet.Tables(0).Rows(mybind.Position).Delete()
        DBSet.Tables(0).AcceptChanges()
    End Sub

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        Me.Close()
    End Sub

    Private Sub cboMZ_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboMZ.SelectedIndexChanged

    End Sub
End Class